﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PizzaService;

namespace PizzaService.View
{
    /// <summary>
    /// Interaction logic for LoginWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();
            this.Loaded += LoginWindow_Loaded;
        }

        void LoginWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = App.Controller.LoginWindowViewModel;
        }
        /// <summary>
        /// mit Enter-Key Login bestätigen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginBoxes_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                LoginButton_Click(new object(), new RoutedEventArgs());
            }
        }
        /// <summary>
        /// Login Button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginButton_Click(object sender, RoutedEventArgs e)
        {
                App.Controller.SetConnectionString(String.Format("server={0};User Id={1};database={2};password={3}", 
                    App.Controller.LoginWindowViewModel.Server, App.Controller.LoginWindowViewModel.User, App.Controller.LoginWindowViewModel.Database, PwdBox.Password));

                if (App.Controller.GenerateModel())
                {
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Connection is not working");
                }
        }
    }
}
